<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form
      ref="userForm"
      label-width="220px"
      :rules="rules"
      :model="userInfo"
    >
      <!--手机 -->

      <el-form-item label="手机">
        <el-input v-model="userInfo.mobile" style="width:220px" />
      </el-form-item>

      <!-- 工号 入职时间 -->

      <el-form-item label="入职时间">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          class="inputW"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <!-- 员工照片 -->

      <el-form-item label="员工头像">
        <!-- 放置上传图片 -->
        <UploadImg v-model="userInfo.staffPhoto" />
      </el-form-item>

      <!-- 保存个人信息 -->
      <el-form-item>
        <el-button type="primary" @click="hSubmit">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getUserDetailById } from '@/api/user.js'
import { saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {
      userId: this.$route.query.id,
      userInfo: {
        mobile: '',
        timeOfEntry: ''
      },
      rules: {
        mobile: [{ pattern: /^1[3-9]\d{9}$/, required: true, message: '请输入正确手机号', triggler: 'blur' }],
        timeOfEntry: [{ required: true, message: '请选择入职日期', triggler: 'change' }]
      }
    }
  },
  created() {
    this.loadUserDetailById()
  },

  methods: {
    async loadUserDetailById() {
      try {
        const res = await getUserDetailById(this.$route.params.id)
        console.log('getUserDetailById', res)
        this.userInfo = res.data
      } catch (err) {
        console.log('getUserDetailById', err)
      }
    },
    async hSubmit() {
      const valid = await this.$refs.userForm.validate().catch(e => e)
      if (!valid) return
      try {
        const res = await saveUserDetailById(this.userInfo)
        console.log(res)
        this.$message.success(res.message)
        this.$router.back()
      } catch (e) {
        this.$message.error(e.message)
      }
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>
